package br.com.android.rtgym;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import br.com.android.rtgym.beans.Aluno;
import br.com.android.rtgym.beans.Usuario;
import br.com.android.rtgym.services.DadosUsuario;
import br.com.android.rtgym.services.TreinosUsuario;

import com.android.rtgym.R;

public class Entrada extends Activity {
	
	private EditText login;
	private EditText password;
	private Button btnEntrar;
	private ProgressDialog progress;
	private Toast aviso;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_entrada);
        
        login = (EditText) findViewById(R.id.login);
        password = (EditText) findViewById(R.id.password);
        btnEntrar = (Button) findViewById(R.id.btnEntrar);
        
        btnEntrar.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				progress = ProgressDialog.show(Entrada.this, "Aguarde", "Verificando dados",true);
				aviso = Toast.makeText(Entrada.this, null, Toast.LENGTH_LONG);
				
				new Thread(new Runnable() {
					public void run() {
						
						Usuario usuario = new Usuario();
		        		
		        		usuario.setLogin(login.getText().toString().trim());
						usuario.setSenha(password.getText().toString());
						
						try {
							Aluno aluno = new DadosUsuario("usuarios").validaLogin(usuario);
							Intent intentTreinoAluno = new Intent(Entrada.this, Treinos.class);
							intentTreinoAluno.putExtra("treinosAluno", new TreinosUsuario("listagem/").getTreinosUsuario(aluno.getIdAluno()));
							intentTreinoAluno.putExtra("aluno", aluno);
							startActivity(intentTreinoAluno);
							
							progress.dismiss();
						} catch(RuntimeException e) {
							progress.dismiss();
							aviso.setText(e.getMessage());
							aviso.show();
						}
						
					}
				}).start();
				
				
        	}
        });
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_entrada, menu);
        return true;
    }

    
}
